package com.dong.pointviewpager.widget

import android.content.Context
import android.util.AttributeSet
import android.util.SparseArray
import android.widget.ImageView.ScaleType
import androidx.viewpager.widget.ViewPager
import com.dong.pointviewpager.adapter.LoopPagerAdapter
import com.dong.pointviewpager.config.LoopViewPagerConfig
import com.dong.pointviewpager.constant.DEFAULT_OVER_CARD_OFFSET
import com.dong.pointviewpager.interfaces.IPagerBean
import com.dong.pointviewpager.interfaces.listener.OnLoopPageChangedListener
import com.dong.pointviewpager.interfaces.listener.OnLoopPagerClickListener
import com.dong.pointviewpager.transformer.OverCardTransformer
import com.dong.pointviewpager.utils.DensityUtils.dp2px
import java.util.*

/**
 * Created by Dong on 2018/3/15.
 */
class OverCardViewPager<T: IPagerBean> @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : ViewPager(context, attrs) {

    var beans  = mutableListOf<T>()
        set(value) {
            beans.clear()
            beans.addAll(value)
            offscreenPageLimit = beans.size + LoopViewPagerConfig.NUM_POSITION_CASE * 2//需要预加载，否则Gallery会有问题
        }
    var offset = 0f
        set(value) {
            field = value
            setPadding(0, 0, 0, value.toInt())
        }

    var pageChangeListener: OnLoopPageChangedListener? = null
    var onLoopPagerClickListener: OnLoopPagerClickListener<T>? = null

    private val config = LoopViewPagerConfig()
    private var loopPagerAdapter: LoopPagerAdapter<T>? = null

    init {
        offscreenPageLimit = 2
        clipToPadding = false
        offset = DEFAULT_OVER_CARD_OFFSET.toFloat()
        setPadding(0, 0, 0, dp2px(getContext(), offset).toInt())
        addOnPageChangeListener(object : OnPageChangeListener {
            override fun onPageScrolled(i: Int, v: Float, i1: Int) {
                pageChangeListener?.onLoopPageScrolled(loopPagerAdapter!!.getDataPosition(i), v, i1)
            }

            override fun onPageSelected(i: Int) {
                pageChangeListener?.onLoopPageSelected(loopPagerAdapter!!.getDataPosition(i))
            }

            override fun onPageScrollStateChanged(state: Int) {
                //循环播放时，滑动到最后或第一页，重新定位
                if (config.isLoop && state == SCROLL_STATE_IDLE) {
                    loopPagerAdapter?.run {
                        if(count > 1){
                            if (currentItem == 0) {
                                setCurrentItem(count - 2, false)
                                return
                            }
                            if (currentItem == count - 1) {
                                setCurrentItem(1, false)
                                return
                            }
                        }
                    }
                }
                pageChangeListener?.onLoopPageScrollStateChanged(state)
            }
        })
    }

    fun initialise() {
        setPageTransformer(true, OverCardTransformer(dp2px(context, offset)))
        loopPagerAdapter = LoopPagerAdapter(context, beans, config).apply {
            onLoopPagerClickListener = onLoopPagerClickListener
        }
        adapter = loopPagerAdapter
        setCurrentItem(loopPagerAdapter!!.getDataPosition(1), false)
    }

    fun setLoop(loop: Boolean) {
        config.isLoop = loop
    }

    fun setImageScale(imageScale: ScaleType?){
        config.imageScale = imageScale!!
    }

    fun setRadius(radius: Float){
        config.radius = dp2px(context, radius)
    }

    fun notifyDataChanged() {
        loopPagerAdapter?.notifyDataSetChanged()
    }
}